package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.SecurityUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctSpaceQualityBo;
import com.rzdata.poct.system.domain.business.po.PoctSpaceQuality;
import com.rzdata.poct.system.domain.business.po.PoctSpaceQualityFile;
import com.rzdata.poct.system.domain.business.vo.PoctSpaceQualityVo;
import com.rzdata.poct.system.mapper.PoctSpaceQualityMapper;
import com.rzdata.poct.system.service.IPoctSpaceQualityFileService;
import com.rzdata.poct.system.service.IPoctSpaceQualityService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 室间质评Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-18
 */
@Service
public class PoctSpaceQualityServiceImpl extends ServicePlusImpl<PoctSpaceQualityMapper, PoctSpaceQuality, PoctSpaceQualityVo> implements IPoctSpaceQualityService {

    @Autowired
    IPoctSpaceQualityFileService iPoctSpaceQualityFileService;

    @Override
    public PoctSpaceQualityVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctSpaceQualityVo> queryPageList(PoctSpaceQualityBo bo) {
        PagePlus<PoctSpaceQuality, PoctSpaceQualityVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctSpaceQualityVo> queryList(PoctSpaceQualityBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctSpaceQuality> buildQueryWrapper(PoctSpaceQualityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctSpaceQuality> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlanNo()), PoctSpaceQuality::getPlanNo, bo.getPlanNo());
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), PoctSpaceQuality::getPlanName, bo.getPlanName());
        lqw.like(StringUtils.isNotBlank(bo.getPlanYear()), PoctSpaceQuality::getPlanYear, bo.getPlanYear());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanRound()), PoctSpaceQuality::getPlanRound, bo.getPlanRound());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctSpaceQuality::getInstrumentNo, bo.getInstrumentNo());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentName()), PoctSpaceQuality::getInstrumentName, bo.getInstrumentName());
        lqw.like(StringUtils.isNotBlank(bo.getReagent()), PoctSpaceQuality::getReagent, bo.getReagent());
        lqw.like(StringUtils.isNotBlank(bo.getStandardSubstance()), PoctSpaceQuality::getStandardSubstance, bo.getStandardSubstance());
        lqw.like(StringUtils.isNotBlank(bo.getCheckMethod()), PoctSpaceQuality::getCheckMethod, bo.getCheckMethod());
        lqw.like(StringUtils.isNotBlank(bo.getOrganizeName()), PoctSpaceQuality::getOrganizeName, bo.getOrganizeName());
        lqw.eq(StringUtils.isNotBlank(bo.getQualityResult()), PoctSpaceQuality::getQualityResult, bo.getQualityResult());
        lqw.eq(bo.getExperimentNum() != null, PoctSpaceQuality::getExperimentNum, bo.getExperimentNum());
        lqw.eq(StringUtils.isNotBlank(bo.getQualityScore()), PoctSpaceQuality::getQualityScore, bo.getQualityScore());
        lqw.gt(bo.getCheckTime() != null, PoctSpaceQuality::getCheckTime, bo.getCheckTime());
        lqw.eq(ObjectUtil.isNotEmpty(bo.getDbStatus()), PoctSpaceQuality::getDbStatus, 1);
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctSpaceQualityBo bo) {
        PoctSpaceQuality add = BeanUtil.toBean(bo, PoctSpaceQuality.class);
        validEntityBeforeSave(add);
        add.setCheckTime(new Date());
        add.setCreateBy(SecurityUtils.getUsername());
        add.setDbStatus(1);
        Boolean result =  save(add);
        if (ObjectUtil.isNotEmpty(bo.getFileList())) {
            insertFile(add.getId(), bo.getFileList());
        }
        return result;
    }

    /**
     * 新增文件
     * @param id
     * @param fileList
     */
    private void insertFile(Long id, List<PoctSpaceQualityFile> fileList) {
        if (ObjectUtil.isNotEmpty(fileList)) {
            for (PoctSpaceQualityFile poctSpaceQualityFile : fileList) {
                poctSpaceQualityFile.setQualityId(id);
                QueryWrapper<PoctSpaceQualityFile> queryWrapper= new QueryWrapper<>();
                queryWrapper.lambda().eq(PoctSpaceQualityFile:: getQualityId, id);
                queryWrapper.lambda().eq(PoctSpaceQualityFile:: getFilePath, poctSpaceQualityFile.getFilePath());
                iPoctSpaceQualityFileService.remove(queryWrapper);
            }
            iPoctSpaceQualityFileService.saveBatch(fileList);
        }
    }

    @Override
    public Boolean updateByBo(PoctSpaceQualityBo bo) {
        PoctSpaceQuality update = BeanUtil.toBean(bo, PoctSpaceQuality.class);
        validEntityBeforeSave(update);
        update.setUpdateBy(SecurityUtils.getUsername());
        update.setUpdateTime(new Date());
        Boolean result = updateById(update);
        if (ObjectUtil.isNotEmpty(bo.getFileList())) {
            insertFile(update.getId(), bo.getFileList());
        }
        return result;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctSpaceQuality entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
